﻿using CloudFranceTriple.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace CloudFranceTriple.Tests.Areas.Forum
{
    [TestClass]
    public class PostControllerTest : UnitTestBase
    {
        private Int32 _unitTestTopicId = 9034;
        private Int32 _unitTestPostId = 61126;

        [TestMethod]
        [TestCategory("Forum.Post")]
        public void PostController_PollItem()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.PostController(GetWebSecurity.Object);

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.PollItem(_unitTestTopicId) as PartialViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Forum.Post")]
        public void PostController_Index()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.PostController(GetWebSecurity.Object);

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.Index(_unitTestTopicId) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Forum.Post")]
        public void PostController_Create_Post_ModelIsValid()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.PostController(GetWebSecurity.Object);

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            var _now = DateTime.UtcNow;
            var Postset = new ForumPostSet
            {
                ForumTopic_Id = _unitTestTopicId,
                PostMessage_Message = "Unit Test Message",
                PostMessage_Author = "UniTesteur",
                PostMessage_AuthorAdressIp = "::1",
                PostMessage_CreationDate = DateTime.UtcNow
            };

            // Act
            var result = controller.Create(Postset) as RedirectToRouteResult;
            UnitTestPostId = Postset.Id;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Forum.Post")]
        public void PostController_CreateWithQuotedPost_Post_ModelIsValid()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.PostController(GetWebSecurity.Object);

            var _controllerContext = base.GetControllerContext;

            _controllerContext.SetupGet(x => x.HttpContext.Request.Form).Returns(new System.Collections.Specialized.NameValueCollection { { "QuotedPostId", "0" } });

            controller.ControllerContext = _controllerContext.Object;
            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            var _now = DateTime.UtcNow;
            var Postset = new ForumPostSet
            {
                ForumTopic_Id = _unitTestTopicId,
                PostMessage_Message = "Unit Test Message",
                PostMessage_Author = "UniTesteur",
                PostMessage_AuthorAdressIp = "::1",
                PostMessage_CreationDate = DateTime.UtcNow
            };




            // Act
            var result = controller.Create(Postset) as RedirectToRouteResult;
            UnitTestPostId2 = Postset.Id;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Forum.Post")]
        public void PostController_Create_Get()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.PostController(GetWebSecurity.Object);

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.Create(_unitTestTopicId) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Forum.Post")]
        public void PostController__Edit_Get()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.PostController(GetWebSecurity.Object);

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.Edit(_unitTestPostId) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Forum.Post")]
        public void PostController__Edit_Post()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.PostController(GetWebSecurity.Object);

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            var _now = DateTime.UtcNow;
            var Postset = new ForumPostSet
            {
                Id = _unitTestPostId,
                ForumTopic_Id = _unitTestTopicId,
                PostMessage_Message = "Unit Test Message",
                PostMessage_Author = "UnitTesteur",
                PostMessage_AuthorAdressIp = "::1",
                PostMessage_CreationDate = DateTime.UtcNow
            };

            // Act
            var result = controller.Edit(Postset) as RedirectToRouteResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Forum.Post")]
        public void PostController_Delete_Get()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.PostController(GetWebSecurity.Object);

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.Delete(_unitTestPostId) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
        }

        [TestMethod]
        [TestCategory("Forum.Post")]
        public void PostController_DeleteConfirmed_Post()
        {
            // Arrange
            var controller = new CloudFranceTriple.WebRole.Areas.Forum.Controllers.PostController(GetWebSecurity.Object);

            controller.ControllerContext = base.GetControllerContext.Object;

            var context =
                new Mock<HttpContextBase>(MockBehavior.Strict);

            controller.Url =
                new UrlHelper(new RequestContext(context.Object,
                new RouteData()), new RouteCollection());

            // Act
            var result = controller.DeleteConfirmed(UnitTestPostId) as RedirectToRouteResult;

            // Assert
            Assert.IsNotNull(result);

            result = controller.DeleteConfirmed(UnitTestPostId2) as RedirectToRouteResult;

            // Assert
            Assert.IsNotNull(result);

        }
    }
}
